package com.village.zpark.controller;

import com.github.pagehelper.PageInfo;
import com.village.zpark.common.ResultMapUtil;
import com.village.zpark.pojo.Farmer;
import com.village.zpark.pojo.Production;
import com.village.zpark.service.ProductionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/production")
public class ProductionController {
    @Autowired
    private ProductionService productionService;




    @RequestMapping
    public String Production(){
        return "/production";
    }

    @RequestMapping(value = "/productionPage")
    public String productionPage(){
        return "/productionPage";
    }

    @RequestMapping(value = "/productionQueryById")
    public Object ProductionQueryById(@RequestParam(name = "id",required = true) Integer id, Model model){
        Production production = productionService.findById(id);
        model.addAttribute("obj",production);
        return "/productionPage";
    }

    @RequestMapping(value = "/productionList")
    @ResponseBody
    public Object ProductionList(){
        List<Production> productionList = productionService.findAll();
        return ResultMapUtil.getHashMapList(productionList);
    }


    @RequestMapping(value = "/addfarmer")
    @ResponseBody
    public Object addProduction(Production production){
        try {
            int i = productionService.addProduction(production);
            return ResultMapUtil.getHashMapSave(i);
        } catch (Exception e) {
            return ResultMapUtil.getHashMapException(e);
        }
    }


    @RequestMapping(value = "/updateproduction")
    @ResponseBody
    public Object updateProduction(Production production){
        try {
            int i = productionService.updateProduction(production);
            return ResultMapUtil.getHashMapSave(i);
        } catch (Exception e) {
            return ResultMapUtil.getHashMapException(e);
        }
    }

    @RequestMapping(value = "/deleteproduction")
    @ResponseBody
    public Object deleteproduction(Integer id){
        try {
            int i = productionService.deleteProductionById(id);
            return ResultMapUtil.getHashMapDel(i);
        } catch (Exception e) {
            return ResultMapUtil.getHashMapException(e);
        }
    }


    @RequestMapping(value = "/productionQueryPage")
    @ResponseBody
    public Map<String, Object> productionQueryPage(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer limit, String param) {
        PageInfo<Production> productionPageInfo = productionService.findPage(page, limit, param);
        Map<String,Object> map=new HashMap<>();
        map.put("code", 0);
        map.put("msg", "查询数据成功");
        map.put("count", productionPageInfo.getTotal());
        map.put("data", productionPageInfo.getList());
        return map;
    }
}


